﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;




namespace Projekt
{
    public partial class new_firm : Form
    {
        private DataTable firmNames;
        private String firmIDInputErrorMsg = "Blank input box";
        private String firmNameInputErrorMsg = "Blank input box";
        private String firmPhoneNumberInputErrorMsg = "";
        private ToolTip tt = new ToolTip();

        public new_firm()
        { 
            InitializeComponent();
        }

        /*  ---------------------------------------------
         *  |           Input validating:               |   
         *  |FirmID(CUI):                               |
         *  |   not exist in database                   |
         *  |   6..10 digit                             |
         *  |   only numeric character between 0 and 9  |
         *  |FirmName:                                  |
         *  |   not exist in database                   |   
         *  |shopPhoneNumber:                           |
         *  |   10 or more digit                        |
         *  |   only numeric character between 0 and 9  |
         *  ---------------------------------------------
         *  */

        private void firmID_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                firmIDInputErrorMsg = "";
                if (CegID.Text.Length > 10 || CegID.Text.Length < 6) firmIDInputErrorMsg = firmIDInputErrorMsg + "\nIncorrect CUI(6..10 digit)";
                try 
                { 
                    Convert.ToInt64(CegID.Text);
                    foreach (DataRow rows in firmNames.Rows)
                    {
                        if (rows["CegID"].ToString() == CegID.Text.ToString()) firmIDInputErrorMsg = firmIDInputErrorMsg + "This firm already exist";
                    }
                    
                    cuiInputWarning.Visible = firmIDInputErrorMsg != "";
                }
                catch (FormatException) { firmIDInputErrorMsg = firmIDInputErrorMsg + "\nIncorrect CUI(only numbers are allowed"; }
                
                
            }
            catch (Exception es) 
            {
                firmIDInputErrorMsg = firmIDInputErrorMsg + "\n" +es.ToString();
                cuiInputWarning.Visible = true;
            }
        }
        private void firmID_Validated(object sender, EventArgs e)
        {
            try
            {
                if (firmIDInputErrorMsg == "")
                {
                    if (firmIDInputErrorMsg=="" && firmNameInputErrorMsg=="" && firmPhoneNumberInputErrorMsg=="") Save.Enabled = true;
                    cuiInputWarning.Visible = false;
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
            finally { Save.Enabled = firmIDInputErrorMsg == "" && firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == ""; }
        }

        private void firmName_Validating(object sender, CancelEventArgs e)
        {
            Dalgen conn = Dalgen.Instance;
            firmNameInputErrorMsg = "";
            if (CNev.Text == "") { firmNameInputErrorMsg = firmNameInputErrorMsg + "Invalid name"; }
            else
            {
                foreach (DataRow rows in firmNames.Rows)
                {
                    if (rows["CNev"].ToString() == CNev.Text.ToString())
                    {
                        firmNameInputErrorMsg = firmNameInputErrorMsg + "This firm already exist";
                        break;
                    }
                }
            }
            nameInputWarning.Visible = firmNameInputErrorMsg != "";
        }
        private void firmName_Validated(object sender, EventArgs e)
        {
            try
            {
                if (firmNameInputErrorMsg == "")
                {
                    if (firmIDInputErrorMsg == "" && firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == "") Save.Enabled = true;
                    nameInputWarning.Visible = false;
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
            finally { Save.Enabled = firmIDInputErrorMsg == "" && firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == ""; }
        }

        private void firmPhoneNumber_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                firmPhoneNumberInputErrorMsg = "";
                if (phoneNumber.Text.ToString().Length > 0)
                {
                    Int64 numberEntered = Int64.Parse(phoneNumber.Text);
                    if (phoneNumber.Text.ToString().Length < 10)
                    {
                        firmPhoneNumberInputErrorMsg = firmPhoneNumberInputErrorMsg + "Phone number is 10 or more digit\n";
                        phoneNumberInputWarning.Visible = true;
                    }
                }
            }
            catch (FormatException)
            {
                phoneNumberInputWarning.Visible = true;
                firmPhoneNumberInputErrorMsg = firmPhoneNumberInputErrorMsg + "Phone number contains only numeric digits";
            }
        }
        private void firmPhoneNumber_Validated(object sender, EventArgs e)
        {
            try
            {
                if (firmPhoneNumberInputErrorMsg == "")
                {
                    phoneNumberInputWarning.Visible = false;
                    if (firmIDInputErrorMsg == "" && firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == "") Save.Enabled = true;
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
            finally { Save.Enabled = firmIDInputErrorMsg == "" && firmNameInputErrorMsg == "" && firmPhoneNumberInputErrorMsg == ""; }
        }

        private void CNev_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }
        private void CegID_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }
        private void CTel_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }


        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                Firm ceg = new Firm();
                String ErrorMsg = "";
                if (ceg.add(CNev.Text, CegID.Text, CCim.Text, phoneNumber.Text, ref ErrorMsg))
                {
                    MessageBox.Show("New firm successfully added");
                    this.Close();
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void new_firm_Load(object sender, EventArgs e)
        {
            try
            {
                Firm ceg = new Firm();
                firmNames = ceg.getFirmList();
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void nameInputWarning_MouseOver(object sender, EventArgs e)
        {
            try
            {
                String ttString = "";
                if (firmNameInputErrorMsg != "") ttString = ttString + "Name: " + firmNameInputErrorMsg + "\n";
                if (firmIDInputErrorMsg != "") ttString = ttString + "CUI: " + firmIDInputErrorMsg + "\n";
                if (firmPhoneNumberInputErrorMsg != "") ttString = ttString + "Phone number: " + firmPhoneNumberInputErrorMsg + "\n";
                tt.Show(ttString, (PictureBox)sender);
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void tooltpit_reset(object sender, EventArgs e)
        {
            tt.RemoveAll();
        }

        private void phoneNumberInputWarning_MouseOver(object sender, EventArgs e)
        {
            tt = new ToolTip();
            tt.Show(firmPhoneNumberInputErrorMsg, nameInputWarning);
        }


    }
}
